package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baijiahulian.common.utils.StringUtils;
import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Course;
import com.bjqh.edu.Bean.OrderDetail;
import com.bjqh.edu.Bean.Orderdetails;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class CanceledDetailActivity extends AppCompatActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;

    Orderdetails.DataBean dataBean;
    OrderDetail.OrderBean orderBean;
    OrderDetail.OrderBean.CourseBean courseBean;
    Course course;
    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.tv_des1)
    TextView tvDes1;
    @BindView(R.id.tv_des2)
    TextView tvDes2;
    @BindView(R.id.bc)
    RelativeLayout bc;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_pp)
    TextView tvPp;
    @BindView(R.id.btn_bay)
    Button btnBay;
    @BindView(R.id.main)
    LinearLayout main;
    String price;
    String coursename;
    String coursedes;
    String cid;
    Double op;
    Double pp;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.tv_pp2)
    TextView tvPp2;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.btn_payment)
    TextView btnPayment;
    @BindView(R.id.pop_layout)
    LinearLayout popLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_canceled_detail);
        ButterKnife.bind(this);
        tvTitle.setText("已支付");
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Bundle bundle = (Bundle) getIntent().getExtras().get("bundle");
        dataBean = (Orderdetails.DataBean) bundle.getParcelable("data");
        OkGo.post(AppConstants.BASE_URL + "/order/detail.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("id", dataBean.getId())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("canceled", s);
                        Gson gson = new Gson();
                        OrderDetail orderDetail = gson.fromJson(s, OrderDetail.class);
                        if (orderDetail.getMsg() != null) {
                            if (orderDetail.getMsg().equals("timeout")) {
                                AppConstants.gotoLogin(CanceledDetailActivity.this);
                            } else if (orderDetail.getMsg().equals("remote login")) {
                                AppConstants.remologin(CanceledDetailActivity.this);
                            }
                            else if (orderDetail.getMsg().equals("ac none"))
                            {
                                btnBay.setText("课程已售完");
                                btnBay.setBackgroundColor(getResources().getColor(R.color.black_gray));
                                btnBay.setEnabled(false);
                                orderBean = orderDetail.getOrder();
                                if(orderBean!=null&&orderBean.getCourse()!=null)
                                {
                                    courseBean = orderBean.getCourse();
                                    if(courseBean.getDiscount()!=null)
                                    {
                                        Float count = Float.parseFloat(courseBean.getDiscount());
                                        pp = count / 10 * courseBean.getPrice();
                                        DecimalFormat df = new DecimalFormat("#0.00");
                                        tvPp.setText("￥" + String.valueOf(df.format(pp)));
                                        price = String.valueOf(df.format(pp));
                                        coursename = courseBean.getName();
                                        coursedes = courseBean.getDes();
                                        cid = courseBean.getId();
                                        String[] strarray = courseBean.getDes().split(",");
                                        tvDes1.setText(strarray[0]);
                                        if (strarray.length > 1)
                                            tvDes2.setText("(" + strarray[1] + ")");
                                        tvName.setText(courseBean.getName());
                                        Glide.with(getApplication())
                                                .load(AppConstants.IMG_URL + courseBean.getImg())
                                                .asBitmap()
                                                .placeholder(R.mipmap.kcgm_bj)
                                                .into(ivBg);
                                    }
                                }
                            }
                            else if(orderDetail.getMsg().equals("ac end"))
                            {
                                btnBay.setText("活动已结束");
                                btnBay.setBackgroundColor(getResources().getColor(R.color.black_gray));
                                btnBay.setEnabled(false);
                                orderBean = orderDetail.getOrder();
                                if(orderBean!=null&&orderBean.getCourse()!=null)
                                {
                                    courseBean = orderBean.getCourse();
                                    if(courseBean.getDiscount()!=null)
                                    {
                                        Float count = Float.parseFloat(courseBean.getDiscount());
                                        pp = count / 10 * courseBean.getPrice();
                                        DecimalFormat df = new DecimalFormat("#0.00");
                                        tvPp.setText("￥" + String.valueOf(df.format(pp)));
                                        price = String.valueOf(df.format(pp));
                                        coursename = courseBean.getName();
                                        coursedes = courseBean.getDes();
                                        cid = courseBean.getId();
                                        String[] strarray = courseBean.getDes().split(",");
                                        tvDes1.setText(strarray[0]);
                                        if (strarray.length > 1)
                                            tvDes2.setText("(" + strarray[1] + ")");
                                        tvName.setText(courseBean.getName());
                                        Glide.with(getApplication())
                                                .load(AppConstants.IMG_URL + courseBean.getImg())
                                                .asBitmap()
                                                .placeholder(R.mipmap.kcgm_bj)
                                                .into(ivBg);
                                    }
                                }
                            }
                            else {
                                orderBean = orderDetail.getOrder();
                                if(orderBean!=null&&orderBean.getCourse()!=null)
                                {
                                    courseBean = orderBean.getCourse();
                                    if(courseBean.getDiscount()!=null)
                                    {
                                        Float count = Float.parseFloat(courseBean.getDiscount());
                                        pp = count / 10 * courseBean.getPrice();
                                        DecimalFormat df = new DecimalFormat("#0.00");
                                        tvPp.setText("￥" + String.valueOf(df.format(pp)));
                                        price = String.valueOf(df.format(pp));
                                        coursename = courseBean.getName();
                                        coursedes = courseBean.getDes();
                                        cid = courseBean.getId();
                                        String[] strarray = courseBean.getDes().split(",");
                                        tvDes1.setText(strarray[0]);
                                        if (strarray.length > 1)
                                            tvDes2.setText("(" + strarray[1] + ")");
                                        tvName.setText(courseBean.getName());
                                        Glide.with(getApplication())
                                                .load(AppConstants.IMG_URL + courseBean.getImg())
                                                .asBitmap()
                                                .placeholder(R.mipmap.kcgm_bj)
                                                .into(ivBg);
                                    }

                                }
                            }
                        }

                        tvDes.setText(dataBean.getCoursedes());
                        tvName2.setText(dataBean.getCoursename() + "X1");
                        tvCount.setText("￥" + String.valueOf(dataBean.getPrice()));
                        tvPp.setText("￥" + String.valueOf(dataBean.getPrice()));
                        tvPp2.setText("￥" + String.valueOf(dataBean.getPrice()));
                        Glide.with(getApplication())
                                .load(AppConstants.IMG_URL + courseBean.getImg())
                                .asBitmap()
                                .placeholder(R.mipmap.kcgm_bj)
                                .into(ivImg);
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                    }
                });
        btnBay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("price",String.valueOf(orderBean.getPrice()));
                intent.putExtra("coursedes", orderBean.getCoursedes());
                intent.putExtra("coursename", orderBean.getCoursename());
                intent.putExtra("cid", String.valueOf(dataBean.getCid()));
                intent.setClass(CanceledDetailActivity.this, PaymentActivity.class);
                CanceledDetailActivity.this.startActivity(intent);
            }
        });
    }
}
